import Vue from 'vue'
import Router from 'vue-router'
import store from '../../src/store/index'
import axios from "axios"
import Login from '@/pages/Login/login' //登陆页
import menuConfig from '@/pages/menuConfig/menuConfig'; // 目录配置
import guideMenu from '@/pages/guideMenu/guideMenu' //导航菜单
import portal from '@/pages/guideMenu/portal' //门户网站
import pageFrame from '@/common/pageFrame' //框架
import dataManageDashboard from '@/pages/dataManage/dashboard/dashboard' //首页
import originData from '@/pages/dataManage/originData/originData' //导航原始数据
import internetData from '@/pages/dataManage/originData/components/internetData' //导航原始数据-互联网公开数据
import shadowgraph from '@/pages/dataManage/originData/components/shadowgraph' //导航原始数据-影像图
import map from '@/pages/dataManage/originData/components/map' //导航原始数据-地形图
import resultData from '@/pages/dataManage/resultData/resultData' //导航成果数据
import GJB from '@/pages/dataManage/resultData/components/GJB' //导航成果数据-GJB导航图
import terminal from '@/pages/dataManage/resultData/components/terminal' //导航成果数据-终端应用导航图
import applicationData from '@/pages/dataManage/applicationData/applicationData' //应用配套数据
import DEM from '@/pages/dataManage/applicationData/components/DEM' //应用配套数据-DEM
import appShadowgraph from '@/pages/dataManage/applicationData/components/shadowgraph' //应用配套数据-影像图
import appMap from '@/pages/dataManage/applicationData/components/map' //应用配套数据-地形图
import plpData from '@/pages/dataManage/applicationData/components/plpData' //应用配套数据-点线面数据
import traceData from '@/pages/dataManage/applicationData/components/traceData' //应用配套数据-轨迹数据
import importPage from '@/pages/dataManage/import/import' //导入
import approval from '@/pages/dataManage/approval/approval' //审批
import dashboard from '@/pages/monitorManage/dashboard/dashboard' //看板
import hostList from '@/pages/monitorManage/setting/hostList' //主机列表
import manageMonitor from '@/pages/monitorManage/setting/components/manageMonitor' //管理监控项
import manageAlarm from '@/pages/monitorManage/setting/components/manageAlarm' //管理告警项
import businessManageDashboard from '@/pages/businessManage/dashboard/dashboard' //首页
import needsManage from '@/pages/businessManage/needsManage/needsManage' //业务需求管理
import needsManagement from '@/pages/businessManage/needsManage/components/needsManage' //需求管理
import needsReport from '@/pages/businessManage/needsManage/components/needsReport' //需求上报
import needsList from '@/pages/businessManage/needsManage/components/needsList' //需求管理
import taskManage from '@/pages/businessManage/taskManage/taskManage' //任务管理
import taskManagement from '@/pages/businessManage/taskManage/components/taskManage' //任务管理
import taskCreate from '@/pages/businessManage/taskManage/components/taskCreate' //任务创建
import areaManage from "../pages/businessManage/taskManage/components/areaManage";//地区管理
import taskApproval from '@/pages/businessManage/taskManage/components/taskApproval' //任务审批
import historyTask from '@/pages/businessManage/taskManage/components/historyTask' //历史任务
import baseManage from '@/pages/businessManage/baseManage/baseManage' //基础信息管理
import projectInformation from '@/pages/businessManage/baseManage/components/projectInformation' //项目信息
import needsClassify from '@/pages/businessManage/baseManage/components/needsClassify' //需求分类信息
import permissionManageDashboard from '@/pages/permissionManage/dashboard/dashboard' //首页
import userList from '@/pages/permissionManage/userManage/userList' //用户列表
import userApproval from '@/pages/permissionManage/userManage/userApproval' //用户审批
import roleList from '@/pages/permissionManage/userManage/roleList' //角色列表
import organizeList from '@/pages/permissionManage/userManage/organizeList' //组织机构列表
import flowArrange from '@/pages/businessManage/flowArrange' //流程编排
import newPage from '@/pages/menuConfig/newPage'; // 新增页面
import global from "@/../static/global.vue"
Vue.use(Router)

const router= new Router({
  routes: [
    {
      // 重定向
      path: '/',
      redirect: '/portal',
      meta: {
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
      },
    },
    {
      // 登陆
      path: '/Login',
      component: Login
    },
    {
      // 导航菜单
      path: '/guideMenu',
      component: guideMenu
    },
    {
      // 门户网站
      path: '/portal',
      component: portal,
      meta: {
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
       },
    },
   {//流程编排
      path: '/flowArrange',
      component: flowArrange
    },
    {//框架
      path: '/internetData',
      component: pageFrame,
      children: [
        {
          // 目录配置
          path: '/menuConfig',
          name: 'menuConfig',
          component: menuConfig,
          meta: { title: '目录配置' }
        },
        {
          // 新增页面
          path: '/newPage',
          name: 'newPage',
          component: newPage,
          meta: { title: '新增页面' }
        },
        {
          // 首页
          path: '/dataManage/dashboard',
          name: 'dataManageDashboard',
          component: dataManageDashboard,
          meta: { title: '首页' }
        },
        {
          // 导航原始数据
          path: '/',
          name: 'originData',
          component: originData,
          meta: { title: '导航原始数据', key: '1' },
          children: [
            {
              // 互联网公开数据
              path: '/internetData',
              name: 'internetData',
              component: internetData,
              meta: { title: '互联网公开数据' }
            },
            {
              // 影像图
              path: '/shadowgraph',
              name: 'shadowgraph',
              component: shadowgraph,
              meta: { title: '影像图' }
            },
            {
              // 地形图
              path: '/map',
              name: 'map',
              component: map,
              meta: { title: '地形图' }
            }
          ]
        },
        {
          // 导航成果数据
          path: 'GJB',
          name: 'resultData',
          component: resultData,
          meta: { title: '导航成果数据', key: '2' },
          children: [
            {
              // GJB导航图
              path: '/GJB',
              name: 'GJB',
              component: GJB,
              meta: { title: 'GJB导航图' }
            },
            {
              // 影像图
              path: '/terminal',
              name: 'terminal',
              component: terminal,
              meta: { title: '终端应用导航图' }
            }
          ]
        },
        {
          // 导航成果数据
          path: 'DEM',
          name: 'applicationData',
          component: applicationData,
          meta: { title: '应用配套数据', key: '3' },
          children: [
            {
              // DEM
              path: '/DEM',
              name: 'DEM',
              component: DEM,
              meta: { title: 'DEM' }
            },
            {
              // 影像图
              path: '/appShadowgraph',
              name: 'appShadowgraph',
              component: appShadowgraph,
              meta: { title: '影像图' }
            },
            {
              // 地形图
              path: '/appMap',
              name: 'appMap',
              component: appMap,
              meta: { title: '地形图' }
            },
            {
              // 点线面数据
              path: '/plpData',
              name: 'plpData',
              component: plpData,
              meta: { title: '点线面数据' }
            },
            {
              // 轨迹数据
              path: '/traceData',
              name: 'traceData',
              component: traceData,
              meta: { title: '轨迹数据' }
            }
          ]
        },

        {
          // 审批
          path: '/approval',
          name: 'approval',
          component: approval,
          meta: { title: '审批' }
        },
        {
          // 看板
          path: '/dashboard',
          name: 'dashboard',
          component: dashboard,
          meta: { title: '看板' }
        },
        {
          // 主机列表
          path: '/hostList',
          name: 'hostList',
          component: hostList,
          meta: { title: '主机列表' }
        },
        {
          // 管理监控项
          path: '/manageMonitor',
          name: 'manageMonitor',
          component: manageMonitor,
          meta: { title: '管理监控项' }
        },
        {
          // 管理告警项
          path: '/manageAlarm',
          name: 'manageAlarm',
          component: manageAlarm,
          meta: { title: '管理告警项' }
        },
        {
          // 首页
          path: '/businessManage/dashboard',
          name: 'businessManageDashboard',
          component: businessManageDashboard,
          meta: { title: '首页' }
        },
        {
          // 业务需求管理
          path: 'needsReport',
          name: 'needsManage',
          component: needsManage,
          meta: { title: '业务需求管理', key: '4' },
          children: [
           {
              // 需求上报
              path: '/needsManagement',
              name: 'needsManagement',
              component: needsManagement,
              meta: { title: '需求管理' }
            },
            {
              // 需求上报
              path: '/needsReport',
              name: 'needsReport',
              component: needsReport,
              meta: { title: '需求上报' }
            },
            {
              // 需求管理
              path: '/needsList',
              name: 'needsList',
              component: needsList,
              meta: { title: '需求管理' }
            }
          ]
        },
        {
          // 任务管理
          path: 'taskCreate',
          name: 'taskManage',
          component: taskManage,
          meta: { title: '任务管理', key: '5' },
          children: [
            {
              // 任务管理
              path: '/taskManagement',
              name: 'taskManagement',
              component: taskManagement,
              meta: { title: '任务管理' }
            },
            {
              // 任务创建
              path: '/taskCreate',
              name: 'taskCreate',
              component: taskCreate,
              meta: { title: '任务创建' }
            },
            {
              // 任务审批
              path: '/taskApproval',
              name: 'taskApproval',
              component: taskApproval,
              meta: { title: '任务审批' }
            },
            {
              // 历史任务
              path: '/historyTask',
              name: 'historyTask',
              component: historyTask,
              meta: { title: '历史任务' }
            }
          ]
        },
        {
          // 地理信息维护
          path: '/areaManage',
          name: 'areaManage',
          component: areaManage,
          meta: { title: '地理信息维护' ,key:'7'},
        },
        {
          // 基础信息管理
          path: 'projectInformation',
          name: 'baseManage',
          component: baseManage,
          meta: { title: '基础信息管理', key: '6' },
          children: [
            {
              // 项目信息
              path: '/projectInformation',
              name: 'projectInformation',
              component: projectInformation,
              meta: { title: '项目信息' }
            },
            {
              // 需求分类信息
              path: '/needsClassify',
              name: 'needsClassify',
              component: needsClassify,
              meta: { title: '需求分类信息' }
            }
          ]
        },
        {
          // 首页
          path: '/permissionManage/dashboard',
          name: 'permissionManageDashboard',
          component: permissionManageDashboard,
          meta: { title: '首页' }
        },
        {
          // 用户列表
          path: '/userList',
          name: 'userList',
          component: userList,
          meta: {title: '用户列表'}
        },
        {//用户列表
          path: '/roleList',
          name: 'roleList',
          component: roleList,
          meta: {title: '角色列表'}
        },
        {//用户列表
          path: '/organizeList',
          name: 'organizeList',
          component: organizeList,
          meta: {title: '组织机构列表'}
        },
        {//用户审批
          path: '/userApproval',
          name: 'userApproval',
          component: userApproval,
          meta: {title: '用户审批'}
        },
      ]
    }
  ]
})
 var cookie = {
      set:function(key,val,time){//设置cookie方法
          var date=new Date(); //获取当前时间
          var expiresHours=time;  //将date设置为n小时以后的时间
          date.setTime(date.getTime()+expiresHours*3600*1000); //格式化为cookie识别的时间
          document.cookie=key + "=" + val +";expires="+date.toGMTString();  //设置cookie
      },
      get:function(key){//获取cookie方法
          /*获取cookie参数*/
         var getCookie = document.cookie.replace(/[ ]/g,"");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
         var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
         var tips;  //声明变量tips
         for(var i=0;i<arrCookie.length;i++){   //使用for循环查找cookie中的tips变量
             var arr=arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
             if(key==arr[0]){  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
                 tips=arr[1];   //将cookie的值赋给变量tips
                 break;   //终止for循环遍历
             }
         }
         return tips;
     }
 }
router.beforeEach((to, from, next) => {
    const url = new URL(window.location);
    if(url.searchParams.has("token")&&!cookie.get('token')){
      cookie.set("token",url.searchParams.get("token"),2)
    }
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if(cookie.get('token')){
          axios.get(global.verifyUrl+'/rest/auth/verify?token='+cookie.get('token'))
            .then(function (response) {
                if(response.data.code==-1){
                  cookie.set('token','',-1)
                  window.location.href = global.verifyUrl+"/?ReturnUrl=" + window.location.href
                }
                 localStorage.setItem("username", response.data.data.username)
                 if(!response.data.data.realName) {
                   localStorage.setItem("realname", '')
                 }
                 else{
                   localStorage.setItem("realname", response.data.data.realName)
                 }
                 localStorage.setItem("userid", response.data.data.id)
                 localStorage.setItem("token", cookie.get('token'))
                 localStorage.setItem("role", response.data.data.role)
                 localStorage.setItem("permissions", response.data.data.permissions)
                 next(); //成功则跳转
            })

        }else {
          window.location.href= global.verifyUrl+"/?ReturnUrl=" + window.location.href
        }
    }
    else {
        next();
    }
})
export default router
